import pandas as pd
import os
import geopandas as gpd
from pandas import DataFrame
# gdf = gpd.read_file('E:/时空数据分析/gba_G2/G2_smp.geojson')
# file_dir = 'E:/时空数据分析/OD/o_monthly'
# all_file_list = os.listdir(file_dir)
# for single_file in all_file_list:
#     # 逐个读取
#     single_data_frame = pd.read_csv(os.path.join(file_dir, single_file), sep=',')
#     df=single_data_frame.groupby('O_GWBH_500').sum()
#     print(single_file[:-4], df.head())
#     joined: DataFrame = pd.merge(gdf, df, how='left', left_on='GWBH_500', right_on='O_GWBH_500', left_index=False,
#                       right_index=False,
#                       sort=False, suffixes=('_1', '_2'), copy=False, indicator=False)
#     joined.fillna(value=0)
#     joined.to_file('E:/时空数据分析/o_monthly_json/' + str(single_file[:-4]) + '.geojson', driver='GeoJSON', encoding='utf-8')
#     print(single_file[:-4], 'OK')
#     del df
#     del single_data_frame
#     del joined
geo_data_bc = gpd.read_file('E:/时空数据分析/Final_shp/大湾区fix.shp')
geo_data_bc=geo_data_bc[['OBJECTID','GWBH_500']]
month_list=list(['201706','201707','201708','201709','201710','201711','201712','201801','201802','201803','201804','201805','201806','201807','201808','201809','201810','201811','201812','201901','201902','201903','201904','201905','201906'])

for month in month_list:
    df=pd.read_csv('E:/时空数据分析/OD/d_weekenday/'+str(month)+'_d_weekenday.csv')
    df=df[['D_GWBH_500','num_total']]
    sj_gdf=pd.merge(geo_data_bc, df, how='left', left_on='GWBH_500', right_on='D_GWBH_500', left_index=False,
                         sort=False,
                         right_index=False, suffixes=('_1', '_2'), copy=False, indicator=False)
    gdf_sj = sj_gdf['num_total'].groupby([sj_gdf['GWBH_500']]).sum()
    geo_data_bc = geo_data_bc.join(gdf_sj, on='GWBH_500')
    geo_data_bc.fillna(value=1)
    geo_data_bc.rename(columns={'num_total': 'ED_D' + str(month) + ''}, inplace=True)


    df = pd.read_csv('E:/时空数据分析/OD/o_weekenday/' + str(month) + '_o_weekenday.csv')
    df = df[['O_GWBH_500', 'num_total']]
    sj_gdf=pd.merge(geo_data_bc, df, how='left', left_on='GWBH_500', right_on='O_GWBH_500', left_index=False,
                         sort=False,
                         right_index=False, suffixes=('_1', '_2'), copy=False, indicator=False)
    gdf_sj = sj_gdf['num_total'].groupby([sj_gdf['GWBH_500']]).sum()
    geo_data_bc = geo_data_bc.join(gdf_sj, on='GWBH_500')
    geo_data_bc.fillna(value=1)
    geo_data_bc.rename(columns={'num_total': 'ED_O' + str(month) + ''}, inplace=True)


    df = pd.read_csv('E:/时空数据分析/OD/d_workday/' + str(month) + '_d_workday.csv')
    df = df[['D_GWBH_500', 'num_total']]
    sj_gdf = pd.merge(geo_data_bc, df, how='left', left_on='GWBH_500', right_on='D_GWBH_500', left_index=False,
                      sort=False,
                      right_index=False, suffixes=('_1', '_2'), copy=False, indicator=False)
    gdf_sj = sj_gdf['num_total'].groupby([sj_gdf['GWBH_500']]).sum()
    geo_data_bc = geo_data_bc.join(gdf_sj, on='GWBH_500')
    geo_data_bc.fillna(value=1)
    geo_data_bc.rename(columns={'num_total': 'DY_D' + str(month) + ''}, inplace=True)


    df = pd.read_csv('E:/时空数据分析/OD/o_workday/' + str(month) + '_o_workday.csv')
    df = df[['O_GWBH_500', 'num_total']]
    sj_gdf = pd.merge(geo_data_bc, df, how='left', left_on='GWBH_500', right_on='O_GWBH_500', left_index=False,
                      sort=False,
                      right_index=False, suffixes=('_1', '_2'), copy=False, indicator=False)
    gdf_sj = sj_gdf['num_total'].groupby([sj_gdf['GWBH_500']]).sum()
    geo_data_bc = geo_data_bc.join(gdf_sj, on='GWBH_500')
    geo_data_bc.fillna(value=1)
    geo_data_bc.rename(columns={'num_total': 'DY_O' + str(month) + ''}, inplace=True)


    print(month, 'OK')


geo_data_bc.to_csv('E:/时空数据分析/Final_shp/F大湾区WK.csv',index=False)
# geo_data_bc.to_file('E:/时空数据分析/Final_shp/F大湾区WK.shp', driver='ESRI Shapefile', encoding='utf-8')